package com.dingcan.biao2;


/*创建数据库基类*/

import java.sql.*;

public class BaseDao {
    static Connection connection=null;
    static PreparedStatement preparedStatement=null;
    static Statement statement=null;
    static ResultSet rs=null;


    /*读取外部数据库的配置信息*/
    static final String PATH="D:\\peizhi\\database.properties";
    static final String URL= ConfigMange.getConfig(PATH).getValue("url");
    static final String USER_NAME= ConfigMange.getConfig(PATH).getValue("user");
    static final String PASS= ConfigMange.getConfig(PATH).getValue("password");
    static final String DRIVER= ConfigMange.getConfig(PATH).getValue("driver");




    //连接数据库
    public static boolean getConnection() {
        try {
            Class.forName(DRIVER);
             connection = DriverManager.getConnection(URL, USER_NAME, PASS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
    /*增删改*/
    public int executeUpdate(String sql,Object[] params){
        int updateRows = 0;
        if (this.getConnection()){
            try{
                preparedStatement = connection.prepareStatement(sql);
                for (int i=0;i<params.length;i++){
                    preparedStatement.setObject(i+1,params[i]);
                }
                updateRows= preparedStatement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return updateRows;
    }
    //查
    public  ResultSet executeSQL(String sql,Object[] params){
        if (this.getConnection()){
            try {
                preparedStatement= connection.prepareStatement(sql);
                for (int i=0;i<params.length;i++){
                    preparedStatement.setObject(i+1,params[i]);
                }
                rs = preparedStatement.executeQuery();

            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        return rs;
    }

}
